﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace TvGame
{
    /// <summary> 
    /// A simple splash screen that fades in/out 
    /// and loads the next screen after fading out 
    /// </summary> 
    class SplashScreen : GameScreen
    {
        ContentManager content;
        Texture2D splashTexture;

        const float timeToStayOnScreen = 1.5f;
        float timer = 0f;

        public SplashScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            splashTexture = content.Load<Texture2D>("Sprites\\Player\\joe");
        }
        public override void UnloadContent()
        {
            content.Unload();
        }
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if (ScreenState == ScreenState.Active)
            {
                //When this screen is fully active, we want to 
                //begin our timer so we know when to fade out 
                float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                timer += elapsed;
                if (timer >= timeToStayOnScreen)
                {
                    //When we've passed the 'timeToStayOnScreen' time, 
                    //we call ExitScreen() which will fade out then 
                    //kill the screen afterwards 
                    ExitScreen();
                }
            }
            else if (ScreenState == ScreenState.TransitionOff)
            {
                if (TransitionPosition == 1)
                {                    
                    ScreenManager.AddScreen(new BackgroundScreen(), null);
                    ScreenManager.AddScreen(new MainMenuScreen(), null);
                }
            }
        }
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
            Vector2 center = new Vector2(fullscreen.Center.X, fullscreen.Center.Y);
            spriteBatch.Begin();

            spriteBatch.Draw(splashTexture, center, null, new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha),
                0f, new Vector2(splashTexture.Width / 2, splashTexture.Height / 2), 1f, SpriteEffects.None, 0f);

            spriteBatch.End();
        }
    }
}